﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class Applications : AdminPage, IApplicationsView
    {
        private ApplicationsPresenter _presenter;

        #region IApplicationsView Members

        public void SetApplications(IList<Domain.Application> applications)
        {
            rpApplications.DataSource = applications;
            rpApplications.DataBind();
        }

        public string ContextTranslationFile
        {
            get { return "Admin/Translations/Applications.xml"; }
        }

        public void DisplayUnableToDeleteAlert(string name)
        {
            string script = "cms.Dialogs.Alert(null, '" + GetText(this, "DeleteAlertHeader") + "', '" +
                            GetText(this, "DeleteAlertMessage").Replace("[[Name]]", name) + "', '" +
                            GetText(this, "DeleteAlertOK") + "');";
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "unableToDeleteAlert", script, true);
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new ApplicationsPresenter(this);
            HeadingText = GetText(this, "Heading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
            lbDelete.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                     "function (status) { " +
                                     "if (status == 'ok') " +
                                     "cms.Lists.ShowPaneOverlay('applicationPaneOverlay');" +
                                     "else " +
                                     "cms.Lists.HidePaneOverlays(); " +
                                     "}, " +
                                     "'" + GetText(this, "DeleteHeader") + "', " +
                                     "'" + GetText(this, "DeleteMessage") + "', " +
                                     "'" + GetText(this, "DeleteOK") + "', " +
                                     "'" + GetText(this, "DeleteCancel") + "', " +
                                     "true, " +
                                     "cms.Lists.CurrentName);";
        }

        protected void rpApplications_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var application = (Domain.Application) e.Item.DataItem;
            var litID = (Literal) e.Item.FindControl("litID");
            var litName = (Literal) e.Item.FindControl("litName");
            var litDescription = (Literal) e.Item.FindControl("litDescription");
            litID.Text = "<span id=\"A" + application.ID + "\">";
            litName.Text = application.Name;
            litDescription.Text = application.Description;
        }
        
        protected void lbDelete_Click(object sender, EventArgs e)
        {
            _presenter.DeleteApplication(int.Parse(hidSelectedApplicationID.Value.Substring(1)));
            upnlApplications.Update();
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initApplicationList",
                                                            "initApplicationList();", true);
        }
    }
}